package org.brisling.common.util;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 
 * <p>Title: ConditionUtil</p>
 * <p>Description: 条件参数工具类</p>
 * <p>Company: tongking</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2016年9月18日 上午12:31:45
 */
public class ConditionUtil {

	//静态实例变量
	private static ConditionUtil cu;
	
	/*
	 * 私有构造器
	 */
	private ConditionUtil(){};
	
	/**
	 * 静态方法获取实例
	 * @return
	 */
	public static ConditionUtil getInstance(){
		if(cu==null){
			cu = new ConditionUtil();
		}		
		return cu;
	}
	
	/**
	 * 获取datatable参数，并按照easyui参数命名进行处理，复用后台方法
	 * @param str
	 * @return
	 */
	public static LinkedHashMap<String,Object> getDataTableConditon(String str){
		
		LinkedHashMap<String,Object> map =null;
		if(str!=null && str.trim().length()>0){
			map = generateConditon(str);
		}
		return map;
	}
	/*
	 * 获取datatable 参数对象方法
	 * @param str
	 * @return
	 */
	private static LinkedHashMap<String,Object> generateConditon(String str){
		String[] _param = str.split("&");
		
		LinkedHashMap<String,Object> pMap = new LinkedHashMap<String,Object>();
		//循环迭代所有参赛
		for(String s:_param){
			if(s!=null){
				//单个参赛解析
				String[] p = s.split("=");
				
				String mapKey = null;
				
				String mapValue = null;
				if(p.length>=1){					
					mapKey = p[0];
					if(p.length==2){					
						mapValue = p[1];					
					}
				}			
				createSingalMap(pMap,mapKey,mapValue);
			}
		}
		
		return pMap;
	}
	
	/*
	 * 按照js数组边界定义进行解析，迭代生成参数方法
	 */
	private static LinkedHashMap<String,Object> createSingalMap(LinkedHashMap<String,Object> map,String param,String value){
		
		int indexS = param.indexOf("[");
		
		int indexE = param.indexOf("]");
		
		int len = param.length();
		
		String _key = null;
		LinkedHashMap<String,Object> m = null;
		
		if(indexS==0){
			//第2..n级参数处理
			//取本级参数名称
			int s = indexS+1;;
			
			int e = indexE;
						
			_key = param.substring(s,e);	
			if((len-1)>indexE){
				//存在下级参数
				if(map.containsKey(_key)){
					//本级map已存在
					m = (LinkedHashMap<String, Object>) map.get(_key);
				}else{
					m = new LinkedHashMap<String, Object>();
				}
				String downKey = param.substring(indexE+1);
				if(downKey==null || downKey.trim().compareTo("")==0){
					System.out.println("param1: " + param + " ,downKey1: " + downKey);
				}
				Map<String,Object> mm = createSingalMap(m,downKey,value);
				map.put(_key, mm);					
				return map;
			}else{
				//本级为最末级参数
				m = map;
				m.put(_key, value);
			}
		}else{
			//第一级参数处理
			if(indexS>0){
				//具有多级参数
				_key = param.substring(0,indexS);
				if(map.containsKey(_key)){
					//本级map已存在
					m = (LinkedHashMap<String, Object>) map.get(_key);
				}else{
					m = new LinkedHashMap<String, Object>();
				}
				
				String downKey = param.substring(indexS,len);
				if(downKey==null || downKey.trim().compareTo("")==0){
					System.out.println("param: " + param + " ,downKey: " + downKey);
				}
				Map<String,Object> mm = createSingalMap(m,downKey,value);
				map.put(_key, mm);
				return map;
			}else{
				//只有一级参数
				m = map;
				m.put(param, value);
			}
						
		}
		return m;
	}

}
